#include "OxygenLanceWidget.h"
#include <QPainter>
#include <QDebug>
#include <QFont>
#include <QPushButton>

OxygenLanceWidget::OxygenLanceWidget(QString title, QWidget *parent)
    : CommonContainer(title, true, 2, parent)
    , m_isEditMode(false)
{
    setupUI();
    setupConnections();
}

OxygenLanceWidget::~OxygenLanceWidget()
{
}

void OxygenLanceWidget::setupUI()
{
    m_contentLayout = new QVBoxLayout();
    m_contentLayout->setContentsMargins(0, 0, 0, 0);
    m_contentLayout->setSpacing(0);
    
    addParameterItem("氧气流量", "0");
    addParameterItem("氧枪高度", "0");
    addParameterItem("读数吹氧量", "0");
    addParameterItem("累计吹氧量", "0");
    addParameterItem("转炉角度", "0");
    addParameterItem("氧气压力", "0");
    
    setContentLayout(m_contentLayout);
}

void OxygenLanceWidget::setupConnections()
{
    connect(this, &CommonContainer::editButtonClicked, this, &OxygenLanceWidget::onEditButtonClicked);
    connect(this, &CommonContainer::editModeChanged, this, &OxygenLanceWidget::onEditModeChanged);
}

void OxygenLanceWidget::addParameterItem(const QString &name, const QString &value)
{
    QWidget *paramWidget = new QWidget(this);
    paramWidget->setStyleSheet("border-image: url(:/img/images/Divider line.png);");
    QHBoxLayout *paramLayout = new QHBoxLayout(paramWidget);
    paramLayout->setContentsMargins(0, 6, 0, 6);
    paramLayout->setSpacing(0);
    
    QLabel *nameLabel = new QLabel(name + ":", this);
    nameLabel->setStyleSheet("color: #BCD7FF;"
                             "font-size: 14px;"
                             "font-weight: medium;");
    
    QLineEdit *valueEdit = new QLineEdit(value, this);
    valueEdit->setStyleSheet("background-color: transparent;"
                             "border: none;"
                             "color: #2FFAFD;"
                             "font-size: 16px;"
                             "font-weight: bold;"
                             "text-align: left;");
    valueEdit->setReadOnly(!m_isEditMode);
    valueEdit->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    connect(valueEdit, &QLineEdit::textChanged, this, &OxygenLanceWidget::onValueChanged);
    
    paramLayout->addWidget(nameLabel);
    paramLayout->addStretch();
    paramLayout->addWidget(valueEdit);
    
    m_contentLayout->addWidget(paramWidget);
    
    m_parameterLayouts[name] = paramLayout;
    m_nameLabels[name] = nameLabel;
    m_valueEdits[name] = valueEdit;
}

void OxygenLanceWidget::setParameterValue(const QString &name, const QString &value)
{
    if (m_valueEdits.contains(name)) {
        m_valueEdits[name]->setText(value);
    }
}

QString OxygenLanceWidget::getParameterValue(const QString &name) const
{
    if (m_valueEdits.contains(name)) {
        return m_valueEdits[name]->text();
    }
    return QString();
}

void OxygenLanceWidget::setEditMode(bool enabled)
{
    if (m_isEditMode == enabled) return;
    
    m_isEditMode = enabled;
    
    for (QLineEdit *edit : m_valueEdits) {
        if (edit) {
            edit->setReadOnly(!enabled);
        }
    }
    
    CommonContainer::setEditMode(enabled);
    
    emit editModeChanged(enabled);
}

bool OxygenLanceWidget::isEditMode() const
{
    return m_isEditMode;
}

void OxygenLanceWidget::onValueChanged()
{
    QLineEdit *sender = qobject_cast<QLineEdit*>(QObject::sender());
    if (!sender) return;
    
    for (auto it = m_valueEdits.begin(); it != m_valueEdits.end(); ++it) {
        if (it.value() == sender) {
            emit parameterValueChanged(it.key(), sender->text());
            break;
        }
    }
}

void OxygenLanceWidget::onEditButtonClicked()
{
}

void OxygenLanceWidget::onEditModeChanged(bool enabled)
{
    m_isEditMode = enabled;
    
    for (QLineEdit *edit : m_valueEdits) {
        if (edit) {
            edit->setReadOnly(!enabled);
        }
    }
    
    emit editModeChanged(enabled);
}
